-- bs_police
-- created by cheny3
-- 蓝鲨警官

return {
    doAction = function(action)
        local pos = action.pos;
        local grid = DungeonM.getGridByPos(pos);
        if grid.type ~= GRID_TYPE_BS_POLICE or
            grid.state == GRID_STATE_DISABLE then
            trace("BishopM", "格子：%d不是蓝鲨警官，不能执行", pos);
            return false;
        end

        -- 设置失效
        grid:changeState(GRID_STATE_DISABLE);

        local changeInfo = grid.change_monster;
        local activePet = ME.user.dbase:query("active_pet");

        for petId, monsterElement in pairs(changeInfo) do
            if petId == activePet then
                -- 格子上的奖励需要干掉
                grid.bonus = nil;

                -- 指定出战，转换为怪物
                DungeonM.transGridToMonster(pos, monsterElement);
                return true;
            end
        end

        -- 给奖励
        local bonusInfo = BuildingBonusM.calcNormalObjectBonus(ME.user, grid);
        local bonusList = bonusInfo["bonus"];
        for _, bonus in pairs(bonusList) do
            BonusM.doBonus(bonus, "bs_police");
        end

        EventMgr.fire(event.BS_POLICE_BONUS, {["pos"] = pos, ["bonus"] = bonusList});
    end,
};
